package com.jianbo.proxy.conf;

import java.util.LinkedHashMap;

import org.noear.snack.ONode;

import com.jianbo.proxy.conf.jsconfig.ApiConfig;
import com.jianbo.proxy.conf.jsconfig.AppConfig;
import com.jianbo.proxy.conf.jsconfig.CacheConfig;
import com.jianbo.proxy.conf.jsconfig.FilterConfig;
import com.jianbo.proxy.conf.jsconfig.ProxyConfig;
import com.jianbo.proxy.conf.jsconfig.StaticWebConf;
import com.jianbo.proxy.service.ProxyConfigService;
import com.jianbo.proxy.util.Debug;
import com.jianbo.proxy.util.FileUtil;
import com.jianbo.proxy.util.JavaApi;
import com.jianbo.proxy.util.JsUtil;
import com.jianbo.proxy.util.JsonUtil;

public class ConfBuilder {

    public static StaticWebConf getStaticWebConf() {
        ArgsConf argsConf = ArgsConf.getInstance();
        String staticConfigFile = argsConf.getStaticConfigFile();
        // 从js文件读取配置
        String config = JsUtil.readJson(staticConfigFile);
        LinkedHashMap<String, String> map = JsonUtil.parseSortMap(config);
        LinkedHashMap<String, String> fullPathMap = new LinkedHashMap<>();
        StaticWebConf conf = new StaticWebConf();
        for(String key : map.keySet()) {
            String path = map.get(key);
            if(path.startsWith("@/")) {
                path = path.replace("@/", argsConf.getFrontDir());
                fullPathMap.put(key, path);
            }
        }
        conf.setStaticPath(fullPathMap);
        conf.setOriginalPath(map);
        return conf;
    }

    public static ProxyConfig getProxyConf() {
        ArgsConf argsConf = ArgsConf.getInstance();
        String proxyConfigFile = argsConf.getProxyConfigFile();
        // String config = JsUtil.readJson(proxyConfigFile);
        // 对js代码进行编码，转json
        
        String jsCode = FileUtil.readString(proxyConfigFile);
        jsCode = JsUtil.outputJsCode(jsCode);
        String config = (String)JsUtil.runFull(jsCode);

        ProxyConfig proxyConf = ProxyConfigService.parseConfig(config);
        Debug.info("代理配置", proxyConf);
        return proxyConf;
    }

    public static FilterConfig getFilterConfig() {
        ArgsConf argsConf = ArgsConf.getInstance();
        String filterConfigFile = argsConf.getFilterConfigFile();
        String config = JsUtil.readJson(filterConfigFile);
        LinkedHashMap<String, String> map = JsonUtil.parseSortMap(config);
        FilterConfig filterConfig = new FilterConfig();
        filterConfig.setFilterMap(map);
        Debug.info("过滤器配置", map);
        return filterConfig;
    }

    public static ApiConfig getApiConfig() {
        ArgsConf argsConf = ArgsConf.getInstance();
        String apiConfigFile = argsConf.getApiConfigFile();
        String config = JsUtil.readJson(apiConfigFile);
        LinkedHashMap<String, String> map = JsonUtil.parseSortMap(config);
        ApiConfig apiConfig = new ApiConfig();
        apiConfig.setApiMap(map);
        Debug.info("Api配置", map);
        return apiConfig;
    }

    public static CacheConfig getCacheConfig() {
        ArgsConf argsConf = ArgsConf.getInstance();
        String cacheConfigFile = argsConf.getCacheConfigFile();
        String config = JsUtil.readJson(cacheConfigFile);
        CacheConfig cacheConfig = ONode.deserialize(config, CacheConfig.class);
        Debug.info("缓存配置", cacheConfig);
        return cacheConfig;
    }

    // public static AppConfig getAppConfig() {
    //     ArgsConf argsConf = ArgsConf.getInstance();
    //     String appConfigFile = argsConf.getAppConfigFile();
    //     String config = JsUtil.readJson(appConfigFile);
    //     Debug.info("应用配置", config);
    //     JavaApi.putGlobalData("appConfig", config);
    //     AppConfig appConfig = ONode.deserialize(config, AppConfig.class);
    //     return appConfig;
    // }

    public static void checkConfigFile() {
        ArgsConf argsConf = ArgsConf.getInstance();
        // String appConfigFile = argsConf.getAppConfigFile();
        String cacheConfigFile = argsConf.getCacheConfigFile();
        String proxyConfigFile = argsConf.getProxyConfigFile();
        String staticConfigFile = argsConf.getStaticConfigFile();
        String apiConfigFile = argsConf.getApiConfigFile();
        String filterConfigFile = argsConf.getFilterConfigFile();
        String[] checkFiles = { cacheConfigFile, proxyConfigFile, staticConfigFile, apiConfigFile, filterConfigFile };
        for(String file : checkFiles) {
            if(!FileUtil.exists(file)) {
                throw new RuntimeException("配置文件不存在：" + file);
            }
        }
        
    }
    
}
